package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.mine.entity.OrderUserAddressDataBean;

import java.text.MessageFormat;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/7.
 */

public class OrderUserAddressAdapter extends BaseRecyclerViewAdapter
        <OrderUserAddressDataBean.DataBean.AddressBean> {

    private Context mContext;

    private OnItemClickListener mOnItemClickListener;

    public OrderUserAddressAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public CommonAddressViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = View.inflate(mContext, R.layout.item_order_address_choose, null);
        return new CommonAddressViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        OrderUserAddressDataBean.DataBean.AddressBean addressBean = mList.get(position);
        if (holder instanceof CommonAddressViewHolder) {
            ((CommonAddressViewHolder) holder).setData(addressBean);
            ((CommonAddressViewHolder) holder).setListener(addressBean);
        }
    }

    public class CommonAddressViewHolder extends RecyclerView.ViewHolder {

        /**
         * 用户名称
         */
        @BindView(R.id.tv_user_address_name)
        TextView mTvUserAddressName;

        /**
         * 联系方式
         */
        @BindView(R.id.tv_user_address_phone)
        TextView mTvUserAddressPhone;

        /**
         * 标记是否为默认地址的标签
         */
        @BindView(R.id.tv_is_default_address)
        TextView mTvIsDefaultAddress;

        /**
         * 地址类型标签
         */
        @BindView(R.id.tv_user_address_tag)
        TextView mTvUserAddressTag;

        /**
         * 收货地址
         */
        @BindView(R.id.tv_user_address)
        TextView mTvUserAddress;

        /**
         * 选择按钮
         */
        @BindView(R.id.iv_address_choose)
        ImageView mIvAddressChoose;

        private CommonAddressViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }

        private void setData(OrderUserAddressDataBean.DataBean.AddressBean bean) {

            // 设置用户姓名
            mTvUserAddressName.setText(bean.getTruename());
            // 设置联系方式
            mTvUserAddressPhone.setText(bean.getTelephone());
            // 设置用户地址
            String province = bean.getProvince();
            String city = bean.getCity();
            String county = bean.getCounty();
            mTvUserAddress.setText(MessageFormat.format("{0} {1} {2} {3}", province, city, county, bean.getAddress()));
            // 设置是否显示默认标签
            mTvIsDefaultAddress.setVisibility(TextUtils.equals(bean.getDef(), "1") ? View.VISIBLE : View.GONE);
            // 设置单选按按钮状态
            mIvAddressChoose.setImageDrawable(bean.isSelected() ?
                    ContextCompat.getDrawable(mContext, R.drawable.ic_radio_on_blue) :
                    ContextCompat.getDrawable(mContext, R.drawable.ic_radio_off_gray)
            );
            // 设置用户标签类型
            mTvUserAddressTag.setVisibility(TextUtils.isEmpty(bean.getLabel()) ? View.GONE : View.VISIBLE);
            mTvUserAddressTag.setText(TextUtils.isEmpty(bean.getLabel()) ? "" : bean.getLabel());
        }

        private void setListener(final OrderUserAddressDataBean.DataBean.AddressBean bean) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemSelected(getAdapterPosition(), bean);
                    }
                }
            });
        }
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
    public interface OnItemClickListener {
        void onItemSelected(int position, OrderUserAddressDataBean.DataBean.AddressBean bean);
    }


}
